//
//  KKBaseTableViewController.swift
//  SinaWeiBo
//
//  Created by King.Kong. on 16/5/23.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit
// MARK:1.遵守协议
class KKBaseTableViewController: UITableViewController,KKVisitorLoginViewDelegate {

    // 切换主页和访客视图
    var userLogin = KKUserAccountViewModel.sharedAccountViewModel.userAccount != nil
    // 定义一个访客视图的属性让 子类都能拿到修改不同的元素
    lazy var visitorLoginView: KKVisitorLoginView = KKVisitorLoginView()
    
    override func loadView() {
        
        if userLogin {
            
            // 显示tableView
            super.loadView()
        } else {
            // 加载访客视图
            loadVisitorView()
        }
        
    }
    
    // 加载访客视图
    private func loadVisitorView() {
        
        view = visitorLoginView
        
        // MARK:2.设置代理
        visitorLoginView.visitorDlegate = self
        
    }
    
    // MARK:3.实现代理方法 跳转控制器
    func userWillLogin(btn:UIButton) {
        
        self.btn = btn
         print("登陆")
        let oauthViewController:KKOAuthViewController = KKOAuthViewController()
        let nav = UINavigationController(rootViewController: oauthViewController)
        nav.transitioningDelegate = self
//        presentViewController(nav, animated: true, completion: nil)
        presentViewController(nav, animated: true, completion: nil)
        performSelector(#selector(KKBaseTableViewController.finishTransition), withObject: nil, afterDelay: 2.0)
    }
    
    func userWillRegister() {
        
         print("注册")
    }

    func finishTransition() {
        
        self.login.stopAnimation()
    }
    var btn:UIButton?
    
    private lazy var login: KKLoginTranslation = KKLoginTranslation(view: self.btn)
    

}

extension KKBaseTableViewController: UIViewControllerTransitioningDelegate {
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.login.reverse = true
        return self.login
    }
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        self.login.reverse = false;
        return self.login;
        
    }
    
}

  